package com.topc.participant.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.topc.base.exception.Asserts;
import com.topc.participant.model.po.RoleMenuMid;
import com.topc.participant.mapper.RoleMenuMidMapper;
import com.topc.participant.model.vo.MenuShowVo;
import com.topc.participant.service.IRoleMenuMidService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色资源中间关系表 服务实现类
 * </p>
 *
 * @author Gogoing
 */
@Slf4j
@Service
public class RoleMenuMidServiceImpl extends ServiceImpl<RoleMenuMidMapper, RoleMenuMid> implements IRoleMenuMidService {


    @Override
    public void updateMenuRole(Long roleId, List<Long> menuIds) {
        LambdaUpdateWrapper<RoleMenuMid> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleMenuMid::getRoleId,roleId);
        boolean remove = this.remove(updateWrapper);
        List<RoleMenuMid> collect = menuIds.stream().map(id -> {
            RoleMenuMid roleMenuMid = new RoleMenuMid();
            roleMenuMid.setRoleId(roleId).setMenuId(id);
            return roleMenuMid;
        }).collect(Collectors.toList());
        boolean save = this.saveBatch(collect);
        if (!save)
            Asserts.fail("增加失败");
    }

    @Override
    public List<Long> getMenuIdByRole(Long roleId) {
        List<Long> menuByRole = this.getBaseMapper().getMenuByRole(roleId);
        return menuByRole;
    }
}
